﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BaseballGame.Models;
using System.Web.Security;
using BaseballGame.Filters;
using WebMatrix.WebData;
using Microsoft.Web.WebPages.OAuth;
using DotNetOpenAuth.AspNet;
using System.Transactions;

namespace BaseballGame.Controllers
{
    public class addPlayerController : Controller
    {
        private game_entitiesDbContext db = new game_entitiesDbContext();
        private UsersContext db1 = new UsersContext();
        //
        // GET: /addPlayer/
        [InitializeSimpleMembership]
        public ActionResult Index()
        {
            var memberId = WebSecurity.GetUserId(User.Identity.Name); // grab ID of current logged in user
        

            int teamid = db.teams.Where(t => t.teamManagerId == memberId).FirstOrDefault().Id;
            var players = db.players.Include(p => p.position).Where(t => t.teamId == teamid);
            return View(players.ToList());
        }

        //
        // GET: /addPlayer/Details/5

        public ActionResult Details(int id = 0)
        {
            Player player = db.players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // GET: /addPlayer/Create

        public ActionResult Create()
        {
            ViewBag.positionId = new SelectList(db.positions, "Id", "position");
            return View();
        }

        //
        // POST: /addPlayer/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Player player)
        {
            if (ModelState.IsValid)
            {
                Random number = new Random();
                player.speed = number.Next(1, 100);
                player.power = number.Next(1, 100);
                player.hitting_chance = number.Next(1, 100);
                player.at_bat = 0;
                player.runs = 0;
                player.hits = 0;
                player.runs_batted_in = 0;
                player.home_runs = 0;
                player.balls_on_balls = 0;
                player.strike_outs = 0;
                player.stolen_bases = 0;
                player.left_on_base = 0;
                player.fielding = number.Next(1, 100);
                player.batting_avg = 0;
                player.pitching = number.Next(1, 100);
                player.innings_pitched = 0.0;
                player.runs_allowed = 0;
                player.earned_runs_allowed = 0;
                player.home_runs_allowed = 0;
                player.walks_hit_by_ip = 0.0;
                player.earned_runs_average = 0.0;


                db.players.Add(player);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.positionId = new SelectList(db.positions, "Id", "position", player.positionId);
            return View(player);
        }

        //
        // GET: /addPlayer/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Player player = db.players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            ViewBag.positionId = new SelectList(db.positions, "Id", "position", player.positionId);
            return View(player);
        }

        //
        // POST: /addPlayer/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Player player)
        {
            if (ModelState.IsValid)
            {
                db.Entry(player).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.positionId = new SelectList(db.positions, "Id", "position", player.positionId);
            return View(player);
        }

        //
        // GET: /addPlayer/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Player player = db.players.Find(id);
            if (player == null)
            {
                return HttpNotFound();
            }
            return View(player);
        }

        //
        // POST: /addPlayer/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Player player = db.players.Find(id);
            db.players.Remove(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}